<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>KG购物广场</title>
  <link rel="stylesheet" href="./layui/css/layui.css">
  <link rel="stylesheet" type="text/css" id= "skin" href="css/site-blue.csss"/>
</head>
<body>
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo layui-hide-xs layui-bg-black" ><a href="index.html" style="color: #FBFBFB">KG购物广场</a></div>
    <!-- 头部区域（可配合layui 已有的水平导航） -->
    <ul class="layui-nav layui-layout-left">
      <!-- 移动端显示 -->
      <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
        <i class="layui-icon layui-icon-spread-left"></i>
      </li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 1</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 2</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 3</a></li>
      <li class="layui-nav-item">
        <a href="javascript:;">皮肤</a>
        <dl class="layui-nav-child skin">
          <dd><a href="javascript:;">默认</a></dd>
          <dd><a href="javascript:;">蓝色</a></dd>
		  <dd><a href="javascript:;">墨绿色</a></dd>
        </dl>
      </li>
    </ul>
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item layui-hide layui-show-md-inline-block">
        <a href="javascript:;">
          <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img">
          <span class = "rootName">管理员</span>
        </a>
        <dl class="layui-nav-child">
          <dd><a class="dd"  href="javascript:;" data-url="root/edit.html">设置</a></dd>
          <dd><a href="javascript:;" id="logout">Sign out</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
        <a href="javascript:;">
          <i class="layui-icon layui-icon-more-vertical"></i>
        </a>
      </li>
    </ul>
  </div>
  
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;"><i class="layui-icon layui-icon-cellphone"></i>  商品管理</a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;" data-url="goods/list.html"><i class="layui-icon layui-icon-cellphone"></i>  商品列表</a></dd>
            <dd><a href="javascript:;" data-url="goods/add.html"><i class="layui-icon layui-icon-add-1"></i>  添加商品</a></dd>
          </dl>
        </li>
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;"><i class="layui-icon layui-icon-senior"></i>  分类管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="sort/list.html"><i class="layui-icon layui-icon-senior"></i>  分类列表</a></dd>
		    <dd><a href="javascript:;" data-url="sort/add.html"><i class="layui-icon layui-icon-add-1"></i>  添加分类</a></dd>
		  </dl>
		</li>
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;"><i class="layui-icon layui-icon-cart"></i>  订单管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="order/list.html"><i class="layui-icon layui-icon-cart"></i>  订单列表</a></dd>
		  </dl>
		</li>
		<li class="layui-nav-item layui-nav-itemed">
		  <a class="" href="javascript:;"><i class="layui-icon layui-icon-group"></i>  用户管理</a>
		  <dl class="layui-nav-child">
		    <dd><a href="javascript:;" data-url="user/list.html"><i class="layui-icon layui-icon-group"></i>  用户列表</a></dd>
		  </dl>
		</li>
      </ul>
    </div>
  </div>
  
  <div class="layui-body">
    <!-- 内容主体区域 -->
	
    <div> 
  
  <div class="layui-footer">
    <!-- 底部固定 style="padding: 15px;">内容主体区域。记得修改 layui.css 和 js 的路径</div>
  </di区域 -->
    底部固定区域
  </div>
</div>
<script src="./layui/layui.js"></script>
<script type="text/javascript" src="./js/cookie.js" charset="UTF-8"></script>
<script>
//JS 
layui.use(['element', 'layer', 'util'], function(){
  var element = layui.element
  ,layer = layui.layer
  ,util = layui.util;
  $ = layui.$;
  
  //切换皮肤
  $(".skin a").click(function(){
	  var skin = $(this).text();
	  if(skin == '默认'){
		   $("#skin").attr("href","");
	  }else if(skin == '墨绿色'){
		  $("#skin").attr("href","css/site-green.css");
		  
	  }else if(skin == '蓝色'){
		  $("#skin").attr("href","css/site-blue.css");
		  
	  }
	  
	  
  })
  
  
  //退出操作
  $("#logout").click(function(){
	  
	  //清除cookie中的token
	  removeCookie('token');
	  //跳转页面
	  window.location.href="login.html";
	  
  });
  
  /* 给菜单绑定点击事件*/
   $(".layui-nav-tree dd>a").click(function(){
  	 //读取data-url
  	 var url = $(this).attr("data-url");//获取属性
  	 //使用Ajax
  	 gotoUrl(url);
  	 
   });
   $(".layui-layout-right dd>.dd").click(function(){
	   
	   //读取data-url
	   var url = $(this).attr("data-url");//获取属性
	   //使用Ajax
	   gotoUrl(url);
   });
  //头部事件
  util.event('lay-header-event', {
    //左侧菜单事件
    menuLeft: function(othis){
      layer.msg('展开左侧菜单的操作', {icon: 0});
    }
    ,menuRight: function(){
      layer.open({
        type: 1
        ,content: '<div style="padding: 15px;">12131</div>'
        ,area: ['260px', '100%']
        ,offset: 'rt' //右上角
        ,anim: 5
        ,shadeClose: true
      });
    }
  });
  
  
  $.ajaxx = $.ajax;  //先取出原来的ajax方法
  $.ajax = function(options){
  	  
  	  if(options.dataType == 'html'){
  		  $.ajaxx(options);
  		  return;
  	  }
  	  //传递token
  	  var token = getCookie('token');
	  
	  if(token != 'null'){
		  if(options.headers == undefined){
		     options.headers = {};
		  }
		  options.headers.token = token;
	  }
  	  
  	  //重写Success的处理
  	  if(options.success == undefined){
  		  options.success = function(data, status, xhr){
  			  
  		  }
  	  }
  	  var success = options.success;
  	  options.success = function(data, status, xhr){
  		  // ...统一判断用户的登录状态
  		  if(data.status == 401){
  			  layer.alert(data.msg, function(){
  				  //跳转登录页面
  				  window.location.href="login.html";
  			  });
  		  }
  		  //获取服务器返回的token
  		  var token = xhr.getResponseHeader('token');
  		  if(token !=null) setCookie('token',token);
  		  
  		  //在调用原来的成功回调
  		  success(data, status, xhr);
  	  }
  	  $.ajaxx(options);
  	  
  } 




checkLogin();
//判断用户是否登录
function checkLogin(){
	//尝试从cookie中读取token
	var token = getCookie("token");
	if(token.length == 0){
		//没登陆
		window.location.href='login.html';
	}else{
		//登录信息过期
		//获取当前登录用户的信息
		$.ajax({
			url:'http://127.0.0.1:8888/api/roots/' + token,
			type:'get',
			dataType:'json',
			success:function(data){
				root = data.data;
				//显示当前用户的名字
				$(".rootName").html(root.realname);
				//用户头像
				if(root.head == null || root.head.length == 0){
					$(".layui-nav-img").attr('src','img/10.gif');
					
				}else{
					$(".layui-nav-img").attr('src','http://127.0.0.1:8888/upload/' + root.head);
				}
				
			},error:function(){
				
				
			}
		});
		
	}
	
}
});  

function gotoUrl(url){
	if(url == undefined){
		alert("连接上缺少必要的data-url")
	}
	//使用Ajax请求url
	$.ajax({
		url:url,
		type:'get',
		data:{},
		dataType:'html',
		success:function(data){
			$(".layui-body>div").html(data);
			
		},error:function(){
			
			
		}
	});
	}
</script>
</body>
</html>
